#!/usr/bin/python
# -*- coding: utf-8 -*-
# @version        : 1.0
# @Create Time    : 2024/8/26 10:03:49
# @File           : crud.py
# @IDE            : PyCharm
# @desc           : 问题反馈

from typing import List
from fastapi import APIRouter, Depends, Path
from utils.response import SuccessResponse
from . import schemas, crud, models, params
from core.dependencies import IdList
from ..taskpool.models import DCTask
from core.crud import construct_filter
from ...base.auth.validate import Auth, AllUserAuth
from sqlalchemy.orm import joinedload

app = APIRouter()


###################################################
#     问题反馈                                     #
###################################################
@app.get("/task/feedback", summary="获取生产任务辅料需求")
async def get_feedback_list(
        p: params.TaskFeedbackParams = Depends(),
        auth: Auth = Depends(AllUserAuth())
):
    v_where = None
    if p.task:
        v_where = construct_filter(DCTask, **p.task)
    del p.task
    datas, count = await crud.TaskFeedbackDal(auth.db).get_datas(
        **p.dict(),
        v_schema=schemas.TaskFeedbackOut,
        v_return_count=True,
        v_join=[["task"]],
        v_options=[joinedload(models.DCTaskFeedback.task)],
        v_where=v_where
    )
    return SuccessResponse(datas, count=count)


@app.get("/task/{ticket_id}/feedback", summary="获取生产任务问题")
async def get_feedback(
        ticket_id: str,
        auth: Auth = Depends(AllUserAuth())
):
    datas, count = await crud.TaskFeedbackDal(auth.db).get_datas(
        v_join=[["task"]],
        v_where=[DCTask.ticket_id == ticket_id],
        v_schema=schemas.TaskFeedbackSimpleOut,
        v_return_count=True
    )
    return SuccessResponse(datas, count=count)


@app.post("/task/feedback", summary="创建生产任务问题")
async def post_feedback(
        data: schemas.TaskFeedback,
        auth: Auth = Depends(AllUserAuth())
):
    data.create_user = auth.user.user_str
    return SuccessResponse(await crud.TaskFeedbackDal(auth.db).create_data(data=data))


@app.post("/task/feedbacks", summary="批量创建生产任务问题")
async def post_feedbacks(
        datas: List[schemas.TaskFeedback],
        auth: Auth = Depends(AllUserAuth())
):
    for data in datas:
        data.create_user = auth.user.user_str
    return SuccessResponse(await crud.TaskFeedbackDal(auth.db).create_datas(datas=[data.model_dump() for data in datas]))


@app.put("/task/feedback/{data_id}", summary="更新生产任务问题")
async def put_feedback(data_id: int, data: schemas.TaskFeedback, auth: Auth = Depends(AllUserAuth())):
    data.update_user= auth.user.user_str
    return SuccessResponse(await crud.TaskFeedbackDal(auth.db).put_data(data_id, data=data))


@app.delete("/task/feedback", summary="批量删除问题", description="软删除")
async def delete_feedback(ids: IdList = Depends(), auth: Auth = Depends(AllUserAuth())):
    await crud.TaskFeedbackDal(auth.db).delete_datas(ids=ids.ids, v_soft=True)
    return SuccessResponse("删除成功")


@app.get("/task/feedback/options", summary="获取问题分类选项")
async def get_feedback_options(auth: Auth = Depends(AllUserAuth())):
    return SuccessResponse(await crud.TaskFeedbackDal(auth.db).get_category_options())
